<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/6/4
 * Time: 14:54
 */

namespace app\admin\controller;


use app\admin\model\ListsList;

class Lists extends Base
{



    public function lists(){
        return $this->fetch();
    }

    public function data($page,$limit){
        $where = input('get.where')?['c_id'=>input('get.where')]:"";
        $data = ListsList::where($where)->order('sort desc')->page($page,$limit)->select()->toArray();
        $count = ListsList::where($where)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }

    public function add(){
        if ($this->request->isPost()){
            $post = input('post.');
            $data = [
                'title'=>$post['title'],
                'image'=>$post['image'],
                'info'=>$post['info'],
                'content'=>$post['content'],
                'is_home'=>$post['is_home'],
                'sort'=>$post['sort']
            ];
            $ret = ListsList::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        return $this->fetch();
    }

    public function edit($id){
        if ($this->request->isPost()){
            $post = input('post.');
            $data = [
                'title'=>$post['title'],
                'image'=>$post['image'],
                'info'=>$post['info'],
                'content'=>$post['content'],
                'is_home'=>$post['is_home'],
                'sort'=>$post['sort']
            ];
            $ret = ListsList::update($data,['id'=>$id]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data = ListsList::where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function del(){
        $id = input('post.id');
        $ret = ListsList::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }
}
